/**
 * 功能描述 列表
 */

define(function (require, exports, module){
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var store =require("store");
	
	var instance = {
		initComponent:function (params,widget){
			var grid = new Grid({
				parentNode:".J-grid",
				autoRender: false,
				model:{
				  url : "api/groupreserve/queryplanItem",
				  params:function(){
					  var subnav = widget.get("subnav");
					  return {
						   "groupReserve.checkInType": "Alliance",
	                		"planItemGroup.alliance":false,
	                		"planItemGroup.servicePoint":subnav.getValue("servicepoint"),
							"planItemGroup.planItem.startDate": subnav.getValue("checkInDate").start,
							"planItemGroup.planItem.startDateEnd": subnav.getValue("checkInDate").end,
							"planItemGroup.checkInStatusIn": subnav.getValue("CheckInStatus"),
						fetchProperties:checkinfile.getGridFetchProperties()
					  }
				  },
				 
			
					columns:[{
						name:"planItemGroup.planItem.startDate",
						label:"年度",
						format: "date",
						formatparams: {
							mode: "YYYY"
						}
					},{
						name: "planItemGroup.planItem.destination.name",
						label: "目的地"
					},{
						name: "planItemGroup.planItem.startDate",
						label: "开始日期",
						format: "date"
					},{
						name: "planItemGroup.planItem.endDate",
						label: "结束日期",
						format: "date"
					},{
						name: "planItemGroup.signUpNum",
						label: "报名人数",
//						format:function(value,row){
//        					if(row.planItemGroup.signUps.length>0){
//                  				return row.planItemGroup.signUps.length;
//                  			}else{
//                  				return "0";
//                  			}
//        				}
					},{
						name: "planItemGroup.allianceOrg.name",
						label: "组团单位",
						format:function(value,row){
							if(row.planItemGroup.alliance==true){
								return row.planItemGroup.allianceOrg.name;
							}else{
								return row.planItemGroup.servicePoint.name;
							}
						}
					},{
						name: "planItemGroup.checkInStatus.value",
						label: "状态",
						format:function(value,row){
        					if(row.planItemGroup.checkInStatus.key=="Reserve"){
                  				return "待确认";
                  			}else if(row.planItemGroup.checkInStatus.key=="CheckIn"){
                  				return "已确认";
                  			}else{
                  				return "";
                  			}
        				}
					},{
						name:"show",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"edit",
							icon:"icon-edit",
							text:"入住确认",
							handler:function(index,data,rowEle){
								var startDate = data.planItemGroup.planItem.startDate;
								var endDate = data.planItemGroup.planItem.endDate;
								if(data.planItemGroup.checkInStatus.key=="CheckIn"){
									Dialog.alert({
				                		title:"提示",
				                		content:"本次入住已经确认过了，不能再确认了！"
				                	});
								}else{
									aw.ajax({
										url:"api/groupreserve/queryall",
										data:{
											"planItemGroup.pkPlanItemGroup":data.planItemGroup.pkPlanItemGroup,
											fetchProperties:"groupReserve.*,groupReserve.operator.*,groupReserve.planItemGroup.*," +
													"groupReserve.planItemGroup.planItem.destination.*," +
													"signUpRegisters.*," +
													"signUpRegisters.signUpDate," +
													"signUpRegisters.checkInDate," +
													"signUpRegisters.checkOutDate," +
													"signUpRegisters.personalInfo.pkPersonalInfo," +
													"signUpRegisters.personalInfo.name," +
													"signUpRegisters.personalInfo.idNumber," +
													"signUpRegisters.personalInfo.mobilePhone," +
													"signUpRegisters.personalInfo.sex.*," +
													"signUpRegisters.travelContract.memberCertificates," +
													"signUpRegisters.travelContract.member.personalInfo.pkPersonalInfo," +
													"signUpRegisters.travelContract.member.personalInfo.name," +
													"signUpRegisters.travelContract.member.personalInfo.idNumber," +
													"signUpRegisters.travelContract.productRule.product.*" 
										},
										dataType:"json",
										success:function(dataold){
											var dat={};
											dat.destination=dataold.groupReserve.planItemGroup.planItem.destination
//											dat.checkInDate=dataold.groupReserve.checkInDate;
//											dat.checkOutDate=dataold.groupReserve.checkOutDate;
											dat.checkInDate=startDate;
											dat.checkOutDate=endDate;
											dat.reserveDate=dataold.groupReserve.reserveDate;
											dat.operator=dataold.groupReserve.operator.name;
											dat.pkUser=dataold.groupReserve.operator.pkUser;
											dat.pkPlanItemGroup=dataold.groupReserve.planItemGroup.pkPlanItemGroup;
											dat.version=dataold.groupReserve.version;
											widget.get("form").setData(dat);
											widget.get("form").setDisabled(true);
											var temp=dataold.signUpRegisters;
											var tem=[];
											for(var i in temp){
												var set={};
												set.personalInfo=temp[i].personalInfo;
												set.travelContract=temp[i].travelContract;
												set.checkInDate=temp[i].checkInDate == null ? dataold.groupReserve.checkInDate : temp[i].checkInDate;
												set.checkOutDate=temp[i].checkOutDate == null ? dataold.groupReserve.checkOutDate : temp[i].checkOutDate;
												set.pkSignUpRegister=temp[i].pkSignUpRegister
												set.version=temp[i].version
												tem.push(set);
											}
											widget.get("optiongrid").setData(tem);
											widget.show([".J-form",".J-editgrid"]).hide([".J-grid"]);
											widget.get("subnav").show(["return","save"]).hide(["servicepoint","CheckInStatus","checkInDate"]);
										}
									});
								}
							}
						},{
							id:"show",
							text:"查看",
							handler:function(index,data,rowEle){
								var startDate = data.planItemGroup.planItem.startDate;
								var endDate = data.planItemGroup.planItem.endDate;
								aw.ajax({
									url:"api/groupreserve/queryall",
									data:{
										"planItemGroup.pkPlanItemGroup":data.planItemGroup.pkPlanItemGroup,
										fetchProperties:"groupReserve.*,groupReserve.operator.*,groupReserve.planItemGroup.*," +
										"groupReserve.planItemGroup.planItem.destination.*," +
										"signUpRegisters.*," +
										"signUpRegisters.signUpDate," +
										"signUpRegisters.checkInDate," +
										"signUpRegisters.checkOutDate," +
										"signUpRegisters.personalInfo.pkPersonalInfo," +
										"signUpRegisters.personalInfo.name," +
										"signUpRegisters.personalInfo.idNumber," +
										"signUpRegisters.personalInfo.mobilePhone," +
										"signUpRegisters.personalInfo.sex.*," +
										"signUpRegisters.travelContract.memberCertificates," +
										"signUpRegisters.travelContract.member.personalInfo.pkPersonalInfo," +
										"signUpRegisters.travelContract.member.personalInfo.name," +
										"signUpRegisters.travelContract.member.personalInfo.idNumber," +
										"signUpRegisters.travelContract.productRule.product.*" 
									},
									dataType:"json",
									success:function(dataold){
										var dat={};
										dat.destination=dataold.groupReserve.planItemGroup.planItem.destination
//										dat.checkInDate=dataold.groupReserve.checkInDate;
//										dat.checkOutDate=dataold.groupReserve.checkOutDate;
										dat.checkInDate=startDate;
										dat.checkOutDate=endDate;
										dat.reserveDate=dataold.groupReserve.reserveDate;
										dat.operator=dataold.groupReserve.operator.name;
										dat.pkUser=dataold.groupReserve.operator.pkUser;
										dat.pkPlanItemGroup=dataold.groupReserve.planItemGroup.pkPlanItemGroup;
										dat.version=dataold.groupReserve.version;
										widget.get("form").setData(dat);
										widget.get("form").setDisabled(true);
										var temp=dataold.signUpRegisters;
										var tem=[];
										for(var i in temp){
											var set={};
											set.personalInfo=temp[i].personalInfo;
											set.travelContract=temp[i].travelContract;
											set.checkInDate=temp[i].checkInDate == null ? dataold.groupReserve.checkInDate : temp[i].checkInDate;
											set.checkOutDate=temp[i].checkOutDate == null ? dataold.groupReserve.checkOutDate : temp[i].checkOutDate;
											set.pkSignUpRegister=temp[i].pkSignUpRegister
											set.version=temp[i].version
											tem.push(set);
										}
										widget.get("optiongrid").setData(tem);
										widget.get("optiongrid").setDisabled(true);
										widget.get("form").setDisabled(true);
										widget.show([".J-form",".J-editgrid"]).hide([".J-grid"]);
										widget.get("subnav").show(["return"]).hide(["servicepoint","CheckInStatus","checkInDate","save"]);
									}
								});
							}
						}]
					}]
				}
			});
			widget.set("grid",grid);
		}
	};
	module.exports = instance;
});
